#region File Description
//-----------------------------------------------------------------------------
// Camera.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace TimeWar
{
    public class Camera
    {
        /// <summary>
        /// A global projection matrix since it never changes
        /// </summary>
        private Matrix projection;

        /// <summary>
        /// A global view matrix since it never changes
        /// </summary>
        private Matrix view;

        private Matrix rotationMatrix;

        /// <summary>
        /// The Camera position which never changes
        /// </summary>
        private Vector3 viewPosition;

        // Camera direction
        // Create a vector pointing the direction the camera is facing.
        private Vector3 transformedReference;

        private Vector3 thirdPersonReference = new Vector3(0, 200, -200);

        protected void updateView()
        {
            view = Matrix.CreateLookAt(viewPosition, Vector3.Zero, Vector3.Forward);

        }

        #region Properties
        public Matrix Projection
        {
            get
            {
                return projection;
            }
        }

        public Matrix View
        {
            get
            {
                return view;
            }
        }

        public Vector3 ViewPosition
        {
            get
            {
                return viewPosition;
            }
            set
            {
                viewPosition = value;
                updateView();
            }
        }
        #endregion

        public Camera(float fov, float nearPlane, float farPlane)
        {
            float aspectRatio = 16f / 9f;

            projection = Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);

            rotationMatrix = Matrix.CreateRotationY(1);
            transformedReference = Vector3.Transform(thirdPersonReference, rotationMatrix);
            //viewPosition = transformedReference + avatarPosition;
            viewPosition = new Vector3(0.0f, 50.0f, 5000.0f);
            updateView();

        }

        public void Update()
        {
            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y < 0)
            {
                viewPosition.Z++;
            }
            else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y > 0)
            {
                viewPosition.Z--;
            }

            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X < 0)
            {
                viewPosition.X++;
            }
            else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X > 0)
            {
                viewPosition.X--;
            }

            updateView();


        }

    }
}

